#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'hello'
__mtime__ = '2019-01-15'
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃　永无BUG！   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""

import time
import requests
from bs4 import BeautifulSoup


time1 = time.time()


# DFA算法
class DFAFilter(object):
    def __init__(self):
        self.keyword_chains = {}  # 关键词链表
        self.delimit = '\x00'  # 限定

    def add(self, keyword):
        keyword = keyword.lower()  # 关键词英文变为小写
        chars = keyword.strip()  # 关键字去除首尾空格和换行
        if not chars:  # 如果关键词为空直接返回
            return
        level = self.keyword_chains
        # 遍历关键字的每个字
        for i in range(len(chars)):
            # 如果这个字已经存在字符链的key中就进入其子字典
            if chars[i] in level:
                level = level[chars[i]]
            else:
                if not isinstance(level, dict):
                    break
                for j in range(i, len(chars)):
                    level[chars[j]] = {}
                    last_level, last_char = level, chars[j]
                    level = level[chars[j]]
                last_level[last_char] = {self.delimit: 0}
                break
        if i == len(chars) - 1:
            level[self.delimit] = 0

    def parse(self, path):
        with open(path) as f:
            for keyword in f:
                self.add(str(keyword).strip())
        print(self.keyword_chains)

    def filter(self, message, repl="#"):
        message = message.lower()
        ret = []
        start = 0
        while start < len(message):
            level = self.keyword_chains
            step_ins = 0
            for char in message[start:]:
                if char in level:
                    step_ins += 1
                    if self.delimit not in level[char]:
                        level = level[char]
                    else:
                        ret.append(repl * step_ins)
                        start += step_ins - 1
                        break
                else:
                    ret.append(message[start])
                    break
            else:
                ret.append(message[start])
            start += 1

        return ''.join(ret)


if __name__ == "__main__":
    gfw = DFAFilter()
    path = "sensitive_words.txt"
    gfw.parse(path)
    url = 'https://mp.weixin.qq.com/s?src=11&timestamp=1547595002&ver=1317&signature=obczFvYcDBQXvTF65xmJdmhjFuH6VjpnWFApvMV*Hv7aPyXgc0faY4jfhf07drRaSygokDf5i5WDLGopLMrDMCWOTL*SP208ohcVTxGTfmJrsRCmDcmJBtHcGHB3gPEh&new=1'

    results = requests.get(url)

    # html = open(results.text, 'r')
    # htmlpage = html.read()
    # soup = BeautifulSoup(htmlpage.strip(), 'html.parser')
    # soup = BeautifulSoup(results.content.strip(), 'html.parser')
    # print(soup.text)


    # print results.content
    # text = "你真是个大傻逼，大傻子，傻大个，大坏蛋，坏人。"
    text = results.content
    print text
    result = gfw.filter(text)

    # print(text)
    print(result)
    time2 = time.time()
    print('总共耗时：' + str(time2 - time1) + 's')
